<template>
  <div style="width: 40%; margin-top: 50px;margin-left: 30%">
    <el-form ref="user" :model="user" label-width="80px">
      <el-form-item label="账户名" prop="name"
                    :rules="[{ required: true, message: '账户不能为空'}]">
        <el-input v-model="user.name" placeholder="请使用英文标点符号或者数字"></el-input>
      </el-form-item>
      <el-form-item label="密码" placeholder="请输入10位及以上" prop="pwd"
                    :rules="[{ required: true, message: '密码不能为空'}]">
        <el-input type="password" v-model="user.pwd" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="收货地址">
        <el-input v-model="user.addr" placeholder="请精确到街道"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码" prop="idCard"
                    :rules="[{ required: true, message: '身份证不能为空'}]">
        <el-input v-model="user.idCard" placeholder="请输入18位身份证号码"></el-input>
      </el-form-item>
      <el-form-item label="姓名"
                    prop="nikeName"
                    :rules="[{ required: true, message: '姓名不能为空'}]"
      >
        <el-input v-model="user.nikeName"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="user.age"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="user.gender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone"
                    :rules="[{ required: true, message: '手机号不能为空'}]">
        <el-input v-model="user.phone" aria-placeholder="请输入11位手机号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('user')">立即注册</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      pwdConfirm: '',
      user: {
        name: '',
        pwd: '',
        addr: '',
        idCard: '',
        phone: '',
        nikeName: '',
        age: '',
        gender: ''
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.register()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //重置
    reset() {
      this.user = ''
    },
    register() {
      if (this.user.gender === '男') {
        this.user.gender = 0
      } else {
        this.user.gender = 1
      }
      this.$axios({
        method: 'post',
        url: '/user/register',
        data: this.user
      }).then(res => {
        alert(res.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
